﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;
using Facebook.Schema;


namespace B12_Ex01_Eran_053094363_Assaf_036722999
{

    public class Search_Manager
    {
        private FacebookManager m_FacebookManager;
        private List<comment> m_PostComments;
        private List<string> m_GenderTypes;
        private Dictionary<stream_post, user> m_PostsUsersTable;
        private const int k_MaxPosts = 100;
        public Search_Manager()
        {
            m_PostsUsersTable = new Dictionary<stream_post, user>();
            m_FacebookManager = new FacebookManager();
            List<comment> m_stream_postcomments = new List<comment>();
        }

        /// <summary>
        /// Gets the stream_post the User selected from the stream_posts collection (Dictionary).
        /// </summary>
        /// <param name="i_stream_postIndex"></param>
        /// <returns></returns>
       public stream_post GetSelectedPost(int i_PostIndex)
        {
            try
            {
                List<stream_post> PostsFound = new List<stream_post>();
                foreach (KeyValuePair<stream_post, user> PostAndUser in m_PostsUsersTable)
                {
                    PostsFound.Add(PostAndUser.Key);
                }
                return PostsFound[i_PostIndex];
            }
            catch (ArgumentOutOfRangeException ex) { return null; }
        }

        public List<string> GetSelectedPostComments(stream_post i_SelectedPost)
        {
            List<string> commentsFound = new List<string>();
            m_PostComments = m_FacebookManager.GetSelectedPostComments(i_SelectedPost);
            foreach (comment comment in m_PostComments)
            {
                commentsFound.Add(comment.text);
            }
            return commentsFound;
        }

        public comment GetSelectedcomment(int i_commentIndex)
        {
            try
            {
                return m_PostComments[i_commentIndex];
            }
            catch (ArgumentOutOfRangeException ex) { return null; }
        } 

        /// <summary>
        /// Gives back the User who wrote the stream_post
        /// </summary>
        /// <param name="i_stream_post"></param>
        /// <returns></returns>
       public user GetPostUser(stream_post i_Post)
        {
            return m_FacebookManager.FacebookService.Users.GetInfo(i_Post.source_id);
        }


        /// <summary>
        /// Gives back the User who wrote the comment
        /// </summary>
        /// <param name="i_comment"></param>
        /// <returns></returns>

        //Gets the stream_posts results from FacebookManager
        //and place them in the dictionary
        public void FilterByPostType(string i_PostsFilter)
        {
            List<stream_post> m_PostsFound = m_FacebookManager.GetPostsByFilter(i_PostsFilter, k_MaxPosts);
            insertPostsToDictionary(m_PostsFound);
        }
        private void insertPostsToDictionary(List<stream_post> i_PostsFound)
        {
            m_PostsUsersTable.Clear();
            foreach (stream_post post in i_PostsFound)
            {
                user postUser = m_FacebookManager.FacebookService.Users.GetInfo(post.actor_id);
                if (postUser != null)
                {
                    m_PostsUsersTable.Add(post, m_FacebookManager.FacebookService.Users.GetInfo(post.actor_id));
                }
            }
        }


        public void FilterByKeyWord(string i_KeyWord)
        {
            Dictionary<stream_post, user> filteredResults = new Dictionary<stream_post, user>();
            foreach (KeyValuePair<stream_post, user> PostAndUser in m_PostsUsersTable)
            {
                if (PostAndUser.Key.message.Contains(i_KeyWord) == true)
                {
                    filteredResults.Add(PostAndUser.Key, PostAndUser.Value);
                }
            }
            m_PostsUsersTable = filteredResults;  
        }

        // -- Properties --//

        public List<string> GenderTypes
        {
            set { m_GenderTypes = value; }
            get { return m_GenderTypes; }
        }

        public FacebookManager FacebookManager
        {
            set { m_FacebookManager = value; }
            get { return m_FacebookManager; }
        }

        public Dictionary<stream_post, user> PostsUsersTable
        {
            set { m_PostsUsersTable = value; }
            get { return m_PostsUsersTable; }
        }

    }
}
